#include "StaticObject.h"
#include "Game.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "Emitter.h"

StaticObject::StaticObject() : Listener(this)
{
	RegisterForEvent("TILECOLLISION");
}

StaticObject::~StaticObject()
{
	delete myEmitter;
}

void StaticObject::Update(float elapsedTime)
{
	if (gravityOn)
		m_ptPosition.y += 50 * elapsedTime;

	if (myEmitter != nullptr)
		myEmitter->Update(elapsedTime);
}
/**************************************************************/
// Render
//	- draw the entity's image at its position
void StaticObject::Render(void)
{

	//SGD::GraphicsManager::GetInstance()->DrawRectangle({ 200, 200, 300, 300 }, { 0, 255, 0 });
	// Draw the image

	SGD::Point position;

	position.x = this->GetPosition().x - Game::GetInstance()->GetCameraPos().x;
	position.y = this->GetPosition().y - Game::GetInstance()->GetCameraPos().y;
	position.x -= m_szSize.width / 2;
	position.y -= m_szSize.height;
	SGD::GraphicsManager::GetInstance()->DrawTexture(
		m_hImage, position,
		m_fRotation, m_szSize);

	if (myEmitter)
		myEmitter->Render();
}


/**************************************************************/
// GetRect
//	- calculate the entity's bounding rectangle
/*virtual*/ SGD::Rectangle StaticObject::GetRect(void) const
{
	SGD::Point position;

	position.x = this->GetPosition().x;
	position.y = this->GetPosition().y;
	position.x -= m_szSize.width / 2;
	position.y -= m_szSize.height;
	return SGD::Rectangle{ position, m_szSize };
}

SGD::Rectangle StaticObject::GetActiveRect(void)	const
{
	return SGD::Rectangle(0, 0, 0, 0);
}


/**************************************************************/
// HandleCollision
//	- respond to collision between entities
/*virtual*/ void StaticObject::HandleCollision(GameObject* pOther)
{
	/* DO NOTHING */
	(void)pOther;		// unused parameter
}

/**************************************************************/
// HandleEvent
//	- respond to collision between tiles
/*virtual*/ void StaticObject::HandleEvent(const SGD::Event* pEvent) /*override;*/
{
	GameObject* object = (GameObject*)pEvent->GetSender();
	StaticObject* sObject = nullptr;
	if (pEvent->GetEventID() == "TILECOLLISION" && (sObject = dynamic_cast<StaticObject*>(object))) // The Event Should be sent if the collision happens from the tile system.
	{
		SGD::Rectangle* collisionRect = (SGD::Rectangle*)pEvent->GetData();
		SGD::Rectangle intersection = sObject->GetRect().ComputeIntersection(*collisionRect);
		//sObject->SetPosition({ sObject->GetPosition().x, intersection.top - 1 });
		sObject->SetPosition({ sObject->GetPosition().x, collisionRect->top - GetRect().ComputeHeight() - 5 });//I THINK THIS FIXES THE CRASH. No promises though.
		sObject->SetVelocity({ 0, 0 });
		sObject->SetGravityState(false);
		//delete collisionRect;
	}
}


/**************************************************************/
// AddRef
//	- increase the reference count
/*virtual*/ void StaticObject::AddRef(void)
{
	++m_unRefCount;
}


/**************************************************************/
// Release
//	- decrease the reference count
//	- self-destruct when the count is 0
/*virtual*/ void StaticObject::Release(void)
{
	--m_unRefCount;

	if (m_unRefCount == 0)
		delete this;
}
